﻿using System.Collections.Generic;
using System.Xml;

namespace YourControl.Resources.Model
{
    /// <summary>
    /// 资源类型
    /// </summary>
    public enum ItemType
    {
        /// <summary>
        /// 图片
        /// </summary>
        Picture = 0,

        /// <summary>
        /// 视频
        /// </summary>
        Video = 1
    }

    /// <summary>
    /// 资源基础类
    /// </summary>
    public class Resources
    {
        private readonly string _filePath;

        public Resources(string filePath)
        {
            _filePath = filePath;
        }

        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }

        private Resources GetResource(XmlNode resource)
        {
            var item = new Resources(_filePath);
            if (resource == null) return null;
            var idElement = resource.SelectSingleNode("id");
            if(idElement != null)
            {
                item.Id = int.Parse(idElement.InnerText);
            }
            var titleElement = resource.SelectSingleNode("title");
            if(titleElement != null)//标题
            {
                item.Title = titleElement.InnerText;
            }
            var itemElement = resource.SelectNodes("items/item");
            if (itemElement == null) return null;
            var items = new List<Item>(); //明细集合
            foreach(XmlNode node in itemElement)
            {
                var myNode = new Item(node);
                items.Add(myNode);
            }
            item.Items = items;
            return item;
        }

        /// <summary>
        /// 资源标题
        /// </summary>
        public string Title { get;  set; }
 
        /// <summary>
        /// 资源明细集合
        /// </summary>
        public List<Item> Items { get;  set; }

        public bool Add(Resources item)
        {
            throw new System.NotImplementedException();
        }

        public bool Delete(int id)
        {
            throw new System.NotImplementedException();
        }

        public bool Update(Resources item)
        {
            throw new System.NotImplementedException();
        }

        public List<Resources> Find()
        {
            var items = new List<Resources>();
            var xmlDocument = new XmlDocument();
            xmlDocument.Load(_filePath);
            var root = xmlDocument.DocumentElement;
            if (root != null)
            {
                var resourcesElement = root.SelectNodes("resource");
                if(resourcesElement != null)
                {
                    foreach(XmlNode node in resourcesElement)
                    {
                        var item = GetResource(node);
                        items.Add(item);
                    }
                }
            }
            return items;
        }

        public string FilePath
        {
            get { return _filePath; }
        }
    }

    /// <summary>
    /// 资源明细
    /// </summary>
    public class Item
    {
        public Item()
        {
            
        }

        public Item(XmlNode itemElement)
        {
            if(itemElement == null) return;
            var thumbnailElement = itemElement.SelectSingleNode("thumbnail");
            if(thumbnailElement != null)
            {
                Thumbnail = thumbnailElement.InnerText;
            }
            var sourceElement = itemElement.SelectSingleNode("source");
            if(sourceElement != null)
            {
                Source = sourceElement.InnerText;
            }
            var typeElement = itemElement.SelectSingleNode("type");
            if(typeElement != null)
            {
                int outType;
                int.TryParse(typeElement.InnerText,out outType);
                Type = (ItemType)outType;
            }
            var isCoverElement = itemElement.SelectSingleNode("isCover");
            if(isCoverElement != null)
            {
                IsCover = bool.Parse(isCoverElement.InnerText);
            }
            var descriptoinElement = itemElement.SelectSingleNode("descriptoin");
            if(descriptoinElement != null)
            {
                Description = descriptoinElement.InnerText;
            }
        }

        /// <summary>
        /// 缩略图
        /// </summary>
        public string Thumbnail { get; set; }

        /// <summary>
        /// 资源类型
        /// </summary>
        public ItemType Type { get;  set; }

        /// <summary>
        /// 是否是封面
        /// </summary>
        public bool IsCover { get; set; }

        /// <summary>
        /// 路径
        /// </summary>
        public string Source { get;  set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get;  set; }
    }
}
